import React, { Component } from 'react'
import ReactDOM from 'react-dom';
import { Modal, Form } from 'antd';
import { ModalForm } from '../../components'
import { fetchData } from '../../components/AjaxConsumer'
import store from '../../stores'
const confirm = Modal.confirm;

const WrappedAdvancedSearchForm = Form.create()(ModalForm);

export default class UpdateSerialrulePage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: false,
            visible: false,
            resData: null,
            submitUrl:'/base/data/serialrule/c'
        };
        //和stroes进行通信挂钩 并绑定解绑事件unsubscribe
        this.unsubscribe = store.listen((storeResData)=>{
            this.setState({
                storeResData: storeResData
            })
        })

        this.formAlerSize = 'large'; //弹出框的大小 只有'large'和'small'两种值 （必须,输错时默认为large）
        this.getFormData = [
            {
                type : 'hiddenInput',
                showName : 'ruleId',
                name : 'ruleId',
                initialValue : ''
            },
            {
                type : 'input',
                showName : '规则代码',
                name : 'ruleCode',
                initialValue : '',
                required:'required'  //选填
            },{
                type : 'input',
                showName : '规则描述',
                name : 'ruleDesc',
                initialValue : '',
                required:'required'
            },{
                type : 'input',
                showName : '长度上限',
                name : 'ruleLength',
                initialValue : '',
                required:'required'
            }
        ];
    }
    //组件销毁时解绑与stores的监听
    componentWillUnmount(){
        this.unsubscribe();
    }
    //显示弹框
    showModal = () => {
        this.setState({
            visible: true,
        });
    }
    // 隐藏弹出框
    hideModal = () => {
        this.setState({
            visible: false,
        });
    }
    // 新增库区
    addSerialruleHandle = () => {
        this.setState({
            submitUrl:'/base/data/serialrule/c',
            formTitle: '新增序列号规则',
            resData: null
        });
        this.showModal();//显示弹框
    }

    // 新增库区
    addInitSerialruleHandle = () => {
        this.setState({resData: null});
        //根据对象的Id 属性获取对象信息
        const options= {
            url: '/base/region/all/',
            type: 'get',//请求方式
            condition:{}
        };
        fetchData(this, options, this.addSerialruleHandle);
    }

    editGetInfoCallBack = (resData) =>{
        this.setState({
            submitUrl:'/base/data/serialrule/u',
            resData: resData
        });
        this.showModal();//显示弹框
    }

    // 编辑信息
    editHandle = (rowObj) => {
        this.setState({resData: null});
        //根据对象的Id 属性获取对象信息
        let ruleId = rowObj.ruleId;
        const options= {
            url: '/base/data/serialrule/r/'+ruleId,
            type: 'get',//请求方式
            condition:{}
        };
        fetchData(this, options, this.editGetInfoCallBack);
    }


    // 删除后的回调
    deleteCallBackHandle = ()=>{
        let result = this.state.resData;
        if (result.code == '0'){
            alert(result.msg);
        }else {
            this.refreshHandle();
        }
    }

    // 删除用户
    deleteHandle = (rowObj) => {
        let ruleId = rowObj.ruleId;

        const options= {
            url: '/base/data/serialrule/d',
            condition:{ruleId:ruleId}
            /*contentType:'application/x-www-form-urlencoded'*/
        };
        let _that = this;
        confirm({
            title: '您确定要删除吗？',
            onOk() {
                fetchData(_that, options, (resData) => {
                    _that.setState({
                        resData: resData
                    });
                    if (resData.code == '0'){
                        alert(resData.msg);
                    }else {
                        _that.refreshHandle();
                    }
                });
            },
            onCancel() {
                console.log("取消了删除操作")
            },
        });
    }


    // 新增/修改后的回调
    handleOk = () => {
        // let result = this.state.resData;
        this.hideModal();
        this.refreshHandle();
    }

    handleCancel = () => {
        this.hideModal();
    }

    //刷新List列表
    refreshHandle = () => {
        console.log("刷新List列表")
        this.props.refreshHandle();
    }

    componentDidMount() {
        const options= {
            url: '/base/region/all',
            type: 'get',
            condition:{}
        };
        fetchData(this, options, this.dicInitCallBackHandle);
    }

    //下拉框 初始化
    dicInitCallBackHandle = (resData)=>{
        //alert('resData' + JSON.stringify(resData, null, 3));
        let optionList = [];
        resData.data.map( (e,index) => {
            let tmp = {};
            tmp.optionKey = e.regionCode;
            tmp.optionShow = e.regionDesc;
            optionList.push(tmp);
        });

        this.getFormData[3].optionList = optionList;
    }

    render() {

        if(this.state.resData != null &&  this.state.resData.data != null){
            let dock = this.state.resData.data;
            this.getFormData.map( (e, index) => {
                e.initialValue = dock[e.name]
            });
        } else {
            this.getFormData.map( (e, index) => {
                if(e.type == 'select'){
                    e.initialValue = '1'
                } else if (e.type == 'datapacker') {
                    e.initialValue = ''
                }
            });
        }

        return(
            <Modal
                style={{top:'20px'}}
                visible={this.state.visible}
                title={this.state.formTitle ? this.state.formTitle : '填写表单'}
                onOk={this.handleOk}
                onCancel={this.handleCancel}
                width={this.formAlerSize=='large'?'850px':'400px'}
                footer=''
            >

                <WrappedAdvancedSearchForm
                    ref="modalFrom"
                    title="序列号规则信息"
                    formAlerSize={this.formAlerSize}
                    formUrl={this.state.submitUrl}
                    getFormData={this.getFormData}
                    closeBox={this.handleCancel}
                    checkBox={this.handleOk} />

            </Modal>
        )
    }
}
